package edu.gatech.startrader.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.RowSpec;

import edu.gatech.startrader.player.Player;
import edu.gatech.startrader.trade.TradeGood;
import edu.gatech.startrader.universe.Planet;
import edu.gatech.startrader.universe.Universe;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JScrollPane;

/**
 * WarpScreen Class
 * This class creates the warp screen that pops up when "Warp" is selected.
 * @author Zohra Barama 
 * @version 1.0
 */
public class WarpScreen extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private final JList travelablePlanetList;
	private final UniversePanel universePanel;
	private final JButton goButton;
	private JTable table;
	private Player player;
	private Universe uni;

	/**
	 * showWarpScreen method
	 * This method shows the warp screen
	 * @param owner the frame
	 * @param play the player
	 * @param uni the current universe
	 */
	public static void showWarpScreen(Frame owner, Player play, Universe uni){
		WarpScreen dialog = new WarpScreen(owner, true, play, uni);
		dialog.setVisible(true);
		owner.repaint();
		return;
	}
	/**
	 * WarpScreen constructor
	 * Create the dialog.
	 * @param owner the frame
	 * @param modal boolean value
	 * @param play the player
	 * @param uni the current universe
	 */
	public WarpScreen(Frame owner, boolean modal, Player play, final Universe uni) 
	{
		super(owner, modal);
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
        }
		player = play;
		this.uni = uni;
		setResizable(false);
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 552, 515);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.NORTH);
		contentPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(113dlu;default)"),
				ColumnSpec.decode("296px:grow"),},
			new RowSpec[] {
				RowSpec.decode("186px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("126dlu"),}));
		{
			universePanel = new UniversePanel(uni);
			universePanel.setBackground(Color.DARK_GRAY);
			contentPanel.add(universePanel, "2, 1, 2, 2, fill, fill");
		}
		{
			JLabel lblPlanets = new JLabel("Planets");
			contentPanel.add(lblPlanets, "2, 3, center, default");
		}
		{
			JLabel lblPrices = new JLabel("Prices");
			contentPanel.add(lblPrices, "3, 3, center, center");
		}
		{
			travelablePlanetList = new JList();
			travelablePlanetList.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) 
				{
					repaint();
				}
			});
			travelablePlanetList.setModel(new AbstractListModel() {
				String[] values = uni.getNamesOfPlanetsInRange();
				public int getSize() {
					return values.length;
				}
				public Object getElementAt(int index) {
					return values[index];
				}
			});
			contentPanel.add(travelablePlanetList, "2, 5, fill, fill");
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			contentPanel.add(scrollPane, "3, 5, fill, fill");
			{
				table = new JTable();
				scrollPane.setViewportView(table);
				table.setRowSelectionAllowed(false);
				table.setModel(new DefaultTableModel(
					new Object[][] {
						{"Water", new Integer(0), new Integer(0)},
						{"Fur", new Integer(0), new Integer(0)},
						{"Food", new Integer(0), new Integer(0)},
						{"Ore", new Integer(0), new Integer(0)},
						{"Games", new Integer(0), new Integer(0)},
						{"Firearms", new Integer(0), new Integer(0)},
						{"Medicine", new Integer(0), new Integer(0)},
						{"Machines", new Integer(0), new Integer(0)},
						{"Narcotics", new Integer(0), new Integer(0)},
						{"Robots", new Integer(0), new Integer(0)},
					},
					new String[] {
						"Resources", "Price", "\u0394Price"
					}
				) {
					Class[] columnTypes = new Class[] {
						Object.class, Integer.class, Integer.class
					};
					public Class getColumnClass(int columnIndex) {
						return columnTypes[columnIndex];
					}
					boolean[] columnEditables = new boolean[] {
							false, false, false
					};
					public boolean isCellEditable(int row, int column) {
						return columnEditables[column];
					}
				});
				table.getColumnModel().getColumn(0).setResizable(false);
				table.getColumnModel().getColumn(1).setResizable(false);
				table.getColumnModel().getColumn(2).setResizable(false);
				table.setColumnSelectionAllowed(true);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				goButton = new JButton("GO!");
				goButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) 
					{
						warp();
					}
				});
				goButton.setEnabled(false);
				goButton.setActionCommand("OK");
				buttonPane.add(goButton);
				getRootPane().setDefaultButton(goButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) 
					{
						setVisible(false);
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		repaint();
	}
	
	
	/**
	 * repaint method
	 * This method repaints after an update
	 */
	public void repaint()
	{
		int index = travelablePlanetList.getSelectedIndex();
		if(index >= 0)
		{
			goButton.setEnabled(true);
			Planet currentlySelectedPlanet = uni.getPlanetInRange(index);
			TradeGood[] goods = TradeGood.values();
			for(int i = 0; i < goods.length; i++)
			{
				table.setValueAt(currentlySelectedPlanet.getPrice(goods[i]), i, 1);
				table.setValueAt(-uni.getCurrentPlanet().getPrice(goods[i]) + currentlySelectedPlanet.getPrice(goods[i]), i, 2);
				if(currentlySelectedPlanet.getPrice(goods[i]) == 0)
				{
					table.setValueAt('-', i, 1);
					table.setValueAt('-', i, 2);
				}
			}	
		}
		else
		{
			goButton.setEnabled(false);
		}
		universePanel.setSelectedIndex(travelablePlanetList.getSelectedIndex());
		super.repaint();
	}
	
	/**
	 * warp method
	 * This method takes the player from the current planet to the selected destination planet
	 */
	public void warp()
	{
		Planet start= uni.getCurrentPlanet();
		Planet end = uni.getPlanetInRange(travelablePlanetList.getSelectedIndex());
		uni.warpPlayer(end);
		player.warp(start, end);
		player.getShip().refuel();
		uni.recalculateTravelablePlanets(player.getShip().getFuel());
		setVisible(false);
		dispose();
	}
}
